class InboxController < ApplicationController
  before_filter :login_required

  def index
    @tasks = Task.inbox_tasks(session['user'])
  end
  
  def review
  end
  
  def next_task
    project_id = params[:id]
    if project_id == nil
      project_id = session[:review_project_id]  
    else
      session[:review_project_id] = project_id
    end
    user  = session['user']
    user_projects = user.active_projects_ids.join(',')
    cond  = "user_id=#{user.id} and done=0 and state='inbox' and task_id=0"
    if project_id != '0'
      cond += " and project_id='#{project_id}'"
    else  
      cond += " and project_id in (#{user_projects})" if user_projects != ''
    end  
    count = Task.count(:conditions=>cond)
    task  = Task.find(:first, :conditions=>cond, :order=>"priority, star desc, `order`")
    if !task
      render(:text=>"Great ! You have no more inbox task to review.")
    else
      render(:partial=>"task", :locals=>{:task=>task, :count=>count})
    end  
  end
  
  def save
    state   = params[:s]
    id      = params[:id]
    begin
      t     = Task.find(id)
      t.state = state
      t.done  = 1 if state=='done'
      t.save
    rescue  
    end  
    redirect_to(:action=>'next_task')
  end
  
end

